/*
 * @lc app=leetcode.cn id=206 lang=javascript
 *
 * [206] 反转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head 单表链结构数据
 * @return {ListNode}
 */
var reverseList = function(head) {
  let prev = null
  let curr = head

  while (curr) {
    const next = curr.next
    curr.next = prev
    prev = curr
    curr = next
  }

  return prev
};
/* 
head = [1, 2, 3, 4, 5]
第一次遍历
next = [2, 3, 4, 5] // curr.next
// 重新指向了curr的next [1]的next为prevnull
curr.next = prev
此时curr为[1]
prev = curr = [1]
curr = [2, 3, 4, 5]

第二次遍历
next = [3, 4, 5] // curr.next
// 重新指向了curr的next [2]的next为prev[1]
curr.next = prev
此时curr为[2, 1]
prev = curr = [2, 1]
curr = [3, 4, 5]


第三次遍历
next = [4, 5] // curr.next
// 重新指向了curr的next [3]的next为prev[2, 1]
curr.next = prev
此时curr为[3, 2, 1]
prev = curr = [3, 2, 1]
curr = [4, 5]

第四次遍历
next = [5] // curr.next
// 重新指向了curr的next [4]的next为prev[3, 2, 1]
curr.next = prev
此时curr为[4, 3, 2, 1]
prev = curr = [4, 3, 2, 1]
curr = [5]

第五次遍历
next = null // curr.next
// 重新指向了curr的next [5]的next为prev[4, 3, 2, 1]
curr.next = prev
此时curr为[5, 4, 3, 2, 1]
prev = curr = [5, 4, 3, 2, 1]
curr = null
*/
// @lc code=end

